<?php
namespace app\home\controller;
use think\Controller;
use think\Captcha;
use think\Db;
use think\Request;
use think\Session;
use think\Validate;

class User extends Controller
{
    //用户注册！
    public function adduser()
    {
        if (request()->isPost()) {

            $data = [
                'username' => input('username'),
                'userpass' => think_weiphp_md5(input('userpass')),
                'mobilenum' => input('mobilenum'),
                'email' => input('email'),
                'address' => input('address'),
                'comment' => input('comment'),
                'createtime' => date("Y-m-d H:i:s"),
            ];

            if($_FILES['pic']['tmp_name']){

                // 获取表单上传文件 例如上传了001.jpg
                $file = request()->file('pic');
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->move(ROOT_PATH . 'public' . DS . 'static/home/uploads/userpic');
                if ($info) {

                    $data['pic'] = date('Ymd') . '/' . $info->getFilename();
                    //echo $data['pic'];die;
                    // 输出 42a79759f284b767dfcb2a0197904287.jpg
                    // echo $info->getFilename();
                } else {
                    // 上传失败获取错误信息
                    $this->error($file->getError());
                }
            }
            $validate = \think\Loader::validate('User');
            if ($validate->check($data)) {
                $db = \think\Db::name('users')->insert($data);
                if ($db) {
                    return $this->success('注册成功', 'Index/index');
                } else {
                    return $this->error('注册失败!');
                }
            } else {
                return $this->error($validate->getError());
            }

            return;
        }
    }

   public function addFoodToCart($food_id,$shop_id,$uid,$num)

  {
  
          
          $data=array();
          if($num==1&&$num!=0)
          {
           $data['food_id']=$food_id;
           $data['shop_id']=$shop_id;
           $data['user_id']=$uid;
           $data['num']=$num;
           $data['shoptime']=date("Y-m-d h:i:sa");
           $re=\think\Db::name('shopcart')->insert($data);
           if ($re) {
             echo "增加1";
           }
           else{
             echo "0";
           }
          }
       else
       {
       $data['num']=$num;
       //$data['shoptime']=date("Y-m-d h:i:sa");
       $res= \think\Db::name('shopcart')->where("user_id='$uid' AND food_id='$food_id'")->setInc('num');
       //echo \think\Db::name('shopcart')->getLastSql();
       if ($res) {
             echo "修改+1";
           }
           else{
             echo "增加失败";
           }
       }
  }
 public function subFoodToCart($food_id,$uid,$num)
  {
    //http://localhost/CampusReservations/public/index.php/home/shop/subFoodToCart/food_id/10/uid/10/num/4
       $data=array();
         if($num==0)
         {

           $re=\think\Db::name('shopcart')->where("user_id='$uid' AND food_id='$food_id'")->delete();
           if ($re) {
             echo "1";
           }
           else{
             echo "0";
           }
         }

       else
       {
        $data['shoptime']=date("Y-m-d h:i:sa");
        $res= \think\Db::name('shopcart')->where("user_id='$uid' AND food_id='$food_id'")->setDec('num');
        if ($res) {
               echo "true";
             }
             else{
               echo "减少失败";
             }
         }
  }


    public function shoppingcart()
    {
      //设置时间为3天，当购物车是的数据超过三天未提交订单，就删除
      $time=60*60*24*3;
      //$addShopTime=null;
      //当前时间
      $shopcart=Db::name('shopcart')->select();
      for ($i=0; $i < sizeof($shopcart); $i++) {
        $nowDate=time(); 
        $addShopTime=strtotime($shopcart[$i]['shoptime']);
        $id=$shopcart[$i]['id'];
        //比较的时间
        $newTime=$nowDate-$addShopTime;
        if ($newTime>$time) {
          Db::name('shopcart')->delete($id);
        }
      }
      $uid = session('id');
      $shoppingcart = \think\Db::name('shopcart')
      ->alias('a')
      ->join('__FOOD__ b ',' a.food_id = b.food_id')
      ->where('a.user_id',$uid)
      ->field('foodname,num,price,b.pic,a.shop_id,a.food_id')
      ->select();
      $this->assign('shoppingcart', $shoppingcart); 
        return $this->fetch();
    }

    //显示用户个人中心
    public function userinfo()
    {
        $uid = session('id');
        $user = \think\Db::name('users')->where('id',$uid)->find();
//        dump($user);
//        die();
        $this->assign('users',$user);

        return $this->fetch();
    }

    //显示修改个人资料
    public function editinfo()
    { $uid = input('id');
        $userinfo = \think\Db::name('users')->where('id',$uid)->find();
        $this->assign('userinfo',$userinfo);
        return $this->fetch();
    }

    //更新个人资料
    public function doUpdate()
    {
        if(request()->isPost()){
            $data=[
                'id'=>input('id'),
                'username'=>input('username'),
                'email'=>input('email'),
                'mobilenum'=>input('mobilenum'),
                'address'=>input('address'),
                'comment'=>input('comment'),
            ];
            if($_FILES['pic']['tmp_name']){
                $_FILES = request()->file('pic');
                $info = $_FILES->validate(['size' => 15678, 'exr' => 'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static/home/uploads/userpic');
                if ($info) {
                    $data['pic'] = date('Ymd') . '/' . $info->getFilename();
                } else {

                    echo $_FILES->getError();
                }

            }
            $volidate = \think\Loader::validate('User');
            if($volidate->check($data)){
                $db = \think\Db::name('users')->update($data);
                if($db){

                    return $this->success('修改成功','userinfo');
                    \think\Session(null);
                    \think\Session::set('id',input('id'));
                    \think\Session::set('username',$data['username']);
                }else{

                    return $this->error('修改失败');
                }

            }
        }
        return $this->fetch();
    }

    //显示订单列表
    public function orderlists()
    {
        $uid = session('id');
        $orderlist = \think\Db::view('order','id,order_id,food_id,sum,user_id,shop_id')
            ->view('food','foodname,food_id','order.food_id=food.food_id')
            ->view('room','state,ordertime,uid,order_id,shop_id','room.order_id=order.order_id')
            ->view('dining','shopname,shop_id,pic','dining.shop_id=order.shop_id')
            ->view('users','id','users.id=order.user_id')
            ->view('message','message,uid','message.uid=order.shop_id')
            ->where('order.user_id',$uid)
            ->distinct(true)
            ->paginate(3);
            dump($orderlist);
            die();
            
      
     $this->assign('orderlist',$orderlist);
     
        return $this->fetch();
    }

    //显示评论列表
    public function commentlists()
    {
        $uid = session('id');//获取当前用户id
        $commentlists = \think\Db::view('message','id,uid,did,mdate,message')
        ->view('dining','shop_id,shopname,pic','message.did=dining.shop_id')
        ->where('uid',$uid)
        ->paginate(3);
        $this->assign('commentlists',$commentlists);
        $reply = \think\Db::view('reply','mid,message,uid,shopid')
        ->view('message','id,uid','reply.mid=message.id')
        ->view('dining','shop_id,shopname','reply.shopid=dining.shop_id')
        ->where('uid',$uid)
        ->select();
//        dump($reply);
//        die();
       $this->assign('reply',$reply);

        // $arr =array();
        // $reply = \think\Db::('')
        // foreach ($commentlists as $k => $v) {
            


        // }
        // dump($commentlists);
        // die();

        return $this->fetch();
    }
    //添加评论
    public function addcomment(){
  \think\Db::name("dining")->where("shop_id='$id'")->setInc("usernum","1");

     $uid = session('id');
     if (request()->isPost()){


   $data=[
  'tastescore'=>input('tastescore'),
  'envscore'=>input('envscore'),
  'servscore'=>input('servscore'),
  'shop_id'=>input('shop_id'),
   ];

   $volidate = \think\Loader::validate('User');
   if ($volidate->check($data)) {
       $db = \think\Db::name('score')->insert($data);
   }
   if ($db) {
     return $this->success('评论成功','userinfo');
   }
   else{

    return $this->error('评论失败','Index/index');
   }



     }


    }
    //删除评论
    public function delcomment(){
    $comment_id = input('comment_id');
      if(db('message')->delete($comment_id)){
        return $this->success('删除成功！','commentlists');
      }else{
        return $this->error('删除失败！','commentlists');
      }
    }



    
}
